from django.db import models
from django.db.models import permalink
from django.conf import settings
from people.models import Person


class Genre(models.Model):
  """ Genre model """
  title         = models.CharField(max_length=100)
  slug          = models.SlugField(prepopulate_from=("title",))
  
  class Meta:
    db_table = 'music_genres'
    ordering = ('title',)

  class Admin:
    pass

  def __str__(self):
    return '%s' % self.title


class Label(models.Model):
  """ Label """
  title         = models.CharField(max_length=100)
  prefix        = models.CharField(max_length=20, blank=True)
  slug          = models.SlugField(prepopulate_from=("title",))
  website       = models.URLField(blank=True, verify_exists=False)

  class Meta:
    db_table = 'music_labels'
    ordering = ('title',)

  class Admin:
    pass

  def __str__(self):
    return '%s' % self.full_title
  
  @property
  def full_title(self):
    return '%s %s' % (self.prefix, self.title)
  
  @permalink
  def get_absolute_url(self):
    return ('music_label_detail', None, { 'slug': self.slug })


class Band(models.Model):
  """ Band """
  title         = models.CharField(max_length=100)
  prefix        = models.CharField(max_length=20, blank=True)
  slug          = models.SlugField(prepopulate_from=("title",))
  musicians     = models.ManyToManyField(Person, blank=True, limit_choices_to={'person_types__slug__exact': 'musician'})
  website       = models.URLField(blank=True, verify_exists=False)

  class Meta:
    db_table = 'music_bands'
    ordering = ('title',)

  class Admin:
    pass

  def __str__(self):
    return '%s' % self.full_title
  
  @property
  def full_title(self):
    return '%s %s' % (self.prefix, self.title)

  @permalink
  def get_absolute_url(self):
    return ('music_band_detail', None, { 'slug': self.slug })


class Album(models.Model):
  """ Listing of music """
  title         = models.CharField(max_length=255)
  prefix        = models.CharField(max_length=20, blank=True)
  subtitle      = models.CharField(blank=True, max_length=255)
  slug          = models.SlugField(prepopulate_from=('title',))
  band          = models.ForeignKey(Band, blank=True)
  label         = models.ForeignKey(Label, blank=True)
  asin          = models.CharField(max_length=14, blank=True)
  released      = models.DateField(blank=True, null=True)
  thumbnail     = models.FileField(upload_to='albums', blank=True)
  review        = models.TextField(blank=True)
  genre         = models.ManyToManyField(Genre, blank=True)

  class Meta:
    db_table = 'music_albums'
    ordering = ('title',)

  class Admin:
    list_display  = ('title', 'band',)

  def __str__(self):
    return '%s' % self.full_title
    
  @permalink
  def get_absolute_url(self):
    return ('music_album_detail', None, { 'slug': self.slug })
  
  @property
  def full_title(self):
    return '%s %s' % (self.prefix, self.title)
        
  @property
  def thumbnail_url(self):
    return '%s%s' % (settings.MEDIA_URL, self.thumbnail)
  
  @property
  def amazon_url(self):
    return 'http://www.amazon.com/dp/%s/' % self.asin


class Song(models.Model):
  """ Songs model """
  album   = models.ForeignKey(Album, blank=True, null=True)
  artist  = models.ForeignKey(Band, blank=True, null=True)
  title   = models.CharField(max_length=255)
  slug    = models.SlugField(prepopulate_from=('title',))
  mp3     = models.FilePathField(path=settings.MEDIA_ROOT+'songs', match='.*\.mp3$')
  
  class Meta:
    db_table = 'music_songs'
    ordering = ('title',)
  
  class Admin:
    pass
  
  def __str__(self):
    return '%s' % self.title
  
  @permalink
  def get_absolute_url(self):
    return ('music_song_detail', None, { 'slug': self.slug })